<template>
  <div class="SystemMessage">
    <el-card>
      <h3><span class="tag"></span> 系统消息</h3>
      <div class="searchBox">
        <span class="txt">ID：</span>
        <el-input v-model="id" clearable size="small"></el-input>
        <span class="margin"></span>
        <span class="txt">内容：</span>
        <el-input v-model="content" clearable size="small"></el-input>
        <span class="margin"></span>
        <span class="txt">时间：</span>
        <el-date-picker
          size="small"
          v-model="timeArr"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="timeOptions"
          value-format="yyyy-MM-dd"
          @change="focusA"
        >
        </el-date-picker>
        <el-button class="btn" icon="el-icon-search" size="small"
          >查询搜索</el-button
        >
      </div>
      <div class="typeBox">
        <el-radio-group v-model="type" size="small" fill="#7b94d8">
          <el-radio-button label="1">未发送</el-radio-button>
          <el-radio-button label="2">已发送</el-radio-button>
        </el-radio-group>
        <div class="right">
          <!-- <el-button icon="el-icon-paperclip" size="small">导出EXCEL</el-button> -->
          <el-button type="success" plain size="small" @click="addPush"
            >新增推送</el-button
          >
        </div>
      </div>
      <el-table
        :data="tableData"
        style="width: 100%; margin-top: 10px"
        :header-cell-style="{ background: '#f5f7fa' }"
      >
        <!-- @selection-change="handleSelectionChange" -->
        <template slot="empty">
          <el-empty :image-size="200"></el-empty>
        </template>
        <!-- <el-table-column type="selection" width="55"> </el-table-column> -->
        <el-table-column prop="date" label="ID"> </el-table-column>
        <el-table-column prop="name" label="标题"> </el-table-column>
        <el-table-column prop="address" label="内容"> </el-table-column>
        <el-table-column prop="address" label="图片"> </el-table-column>
        <el-table-column prop="address" label="链接"> </el-table-column>
        <el-table-column prop="address" label="投送时间"> </el-table-column>
        <el-table-column prop="address" label="投送用户"> </el-table-column>
        <el-table-column prop="address" label="消息路径"> </el-table-column>
        <el-table-column prop="address" label="点击率"> </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="small"
              plain
              @click="pass(scope.row)"
              >通过</el-button
            >
            <el-button
              type="info"
              size="small"
              plain
              @click="operation(scope.row)"
              >不通过</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <br />
      <!-- <el-button size="small" class="btn plcz">批量操作</el-button> -->
      <pagination
        :total="total"
        :page.sync="page"
        :limit.sync="pageSize"
        @pagination="getList"
      />
    </el-card>
    <el-dialog :title="title" :visible.sync="show" width="40%" center>
      <el-form label-position="right" label-width="80px" :model="form">
        <el-form-item label="标题">
          <el-input
            placeholder="请填写标题"
            v-model="form.title"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input
            placeholder="请填写内容"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 10 }"
            v-model="form.content"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="投放用户">
          <el-radio-group v-model="form.userType">
            <el-radio label="1">所有用户</el-radio>
            <el-radio label="2">男用户</el-radio>
            <el-radio label="3">女用户</el-radio>
            <el-radio label="4">主播</el-radio>
            <el-radio label="5">指定用户</el-radio>
          </el-radio-group>
          <el-input
            placeholder="请输入指定用户ID"
            v-show="form.userType == 5"
            v-model="form.setUserId"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="推送方式">
          <el-radio-group v-model="form.pushType">
            <el-radio label="1">站内</el-radio>
            <el-radio label="2">站外</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="推送时间">
          <el-radio-group v-model="form.timeType" @change="form.setTime = ''">
            <el-radio label="1">立即推送</el-radio>
            <el-radio label="2">定时推送</el-radio>
          </el-radio-group>
          <el-date-picker
            v-model="form.setTime"
            type="datetime"
            size="small"
            :disabled="form.timeType != 2"
            style="margin-left: 20px"
            placeholder="选择日期时间"
            default-time="12:00:00"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            :picker-options="pickerOptions1"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="添加图片">
          <el-upload
            class="avatar-uploader"
            :http-request="upLoad"
            :show-file-list="false"
            action
          >
            <div class="imgbox" v-if="form.imageUrl">
              <img :src="form.imageUrl" class="avatar" />
              <!-- <i class="el-icon-close" @click.stop="delImg"></i> -->
              <div class="txt" @click.stop="">{{ form.imageUrl }}</div>
            </div>
            <i v-else class="upicon el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="添加链接">
          <el-input
            placeholder="请填写链接(以http或https开头)"
            v-model="form.link"
            clearable
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="show = false">取 消</el-button>
        <el-button size="small" class="btn" type="primary" @click="show = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination"; // 分页
import { client } from "@/utils/oss.js";
import { ossToken } from "@/api/NotificationManagement";
export default {
  components: { Pagination },
  data() {
    return {
      timeArr: [],
      startDate: "",
      endDate: "",
      id: "",
      content: "",
      type: "1",
      tableData: [{ sd: 1 }, { cs: 2 }],
      total: 0,
      page: 1,
      pageSize: 10,
      //   multipleSelection: [],
      show: false,
      title: "",
      form: {
        title: "",
        content: "",
        resource: "",
        timeType: "",
        setTime: "",
        imageUrl: "",
        link: "",
        pushType: "1",
        userType: "1",
        setUserId: "",
      },
      timeOptions: {
        disabledDate(platformValue) {
          return platformValue.getTime() > Date.now() - 8.64e6;
        },
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 6);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      pickerOptions1: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7;
        },
      },
    };
  },
  methods: {
    //选着时间
    focusA(time) {
      if (!this.timeArr) {
        this.timeArr = [];
      }
      this.startDate = this.timeArr[0];
      this.endDate = this.timeArr[1];
    },
    //选择表格
    // handleSelectionChange(value) {
    //   this.multipleSelection = value;
    //   console.log(this.multipleSelection);
    // },
    //处理不通过
    operation(obj) {
      //   this.show = true;
    },
    //新增推送
    addPush() {
      this.title = "系统消息发布";
      this.show = true;
    },
    //通过
    pass(obj) {},
    getList() {},
    //上传图片
    //上传
    async upLoad(file) {
      await ossToken({
        type: 0,
      }).then((res) => {
        if (res.code == 0) {
          this.dataObj = {
            accessKeyId: res.data.accessKey,
            accessKeySecret: res.data.accessSecret,
            securityToken: res.data.token,
            region: "oss-cn-hangzhou",
            bucket: res.data.bucket,
          };
        }
      });
      const self = this;
      let fileNames =
        "/img/" +
        Math.round(new Date() / 1000) +
        "." +
        file.file.name.substring(file.file.name.lastIndexOf(".") + 1);
      console.log(fileNames);
      client(this.dataObj)
        .multipartUpload(fileNames, file.file)
        .then((result) => {
          //下面是如果对返回结果再进行处理，根据项目需要
          this.form.imageUrl = "https://img.psychexy.com" + fileNames;
          self.$message({
            message: "上传成功",
            type: "success",
          });
        })
        .catch((err) => {
          self.$message.error("上传失败");
        });
    },
  },
};
</script>

<style lang="less" scoped>
.SystemMessage {
  padding: 20px;
  h3 {
    margin: 0 0;
    display: flex;
    align-items: center;
    .tag {
      display: inline-block;
      width: 5px;
      height: 18px;
      background-color: #7b94d8;
      margin-right: 10px;
    }
  }
  .searchBox {
    display: flex;
    align-items: center;
    margin: 20px 0;
    .margin {
      margin: 0 10px;
    }
    .txt {
      display: inline-block;
      height: 32px;
      background-color: #f5f7fa;
      line-height: 32px;
      padding-left: 10px;
      color: #9d9399;
      border-radius: 4px 0 0 4px;
      border: 1px solid #dcdfe6;
      border-right: none;
    }
    /deep/ .el-input__inner {
      border-radius: 0 4px 4px 0 !important;
    }
    .el-input {
      width: 200px;
    }
  }
  .typeBox {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  //   .plcz {
  //     margin-left: 0 !important;
  //   }
  .upicon {
    display: inline-block;
    width: 178px;
    height: 178px;
    line-height: 178px;
    font-size: 24px;
    border: 1px dashed #ccc;
    border-radius: 8px;
  }
  .imgbox {
    position: relative;
    .avatar {
      display: inline-block;
      width: 178px;
      height: 178px;
      line-height: 178px;
      font-size: 24px;
      border: 1px dashed #ccc;
      border-radius: 8px;
      padding: 2px;
    }
    i {
      position: absolute;
      top: -10px;
      right: 40px;
      font-size: 24px;
      font-weight: 700;
      color: #000;
    }
  }
}
</style>